﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using FluentScheduler;
using Vms.Web.Lib;

namespace Vms.Web.Services.Tasks
{
    public class BoardTasks : ITask, IRegisteredObject
    {
        private readonly object _lock = new object();
        private bool _shuttingDown;

        public BoardTasks()
        {
            // Register this task with the hosting environment. Allows for a more graceful stop of the task, in the case of IIS shutting down.
            HostingEnvironment.RegisterObject(this);

        }

        public void Execute()
        {
            lock (_lock)
            {
                if (_shuttingDown)
                    return;
                BoardServices.GetAllScreensStatus();
            }
        }

        public void Stop(bool immediate)
        {
            // Locking here will wait for the lock in Execute to be released until this code can continue.
            lock (_lock)
            {
                _shuttingDown = true;
            }
            HostingEnvironment.UnregisterObject(this);
        }
    }
}